package com.mybatisplus.demo.android.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mybatisplus.demo.android.dao.HeaderFileMapper;
import com.mybatisplus.demo.android.enmu.ApiResponse;
import com.mybatisplus.demo.android.enmu.ResultCode;
import com.mybatisplus.demo.android.model.HeaderInfo;
import com.mybatisplus.demo.android.service.HeaderFileService;
import com.mybatisplus.demo.android.util.LoggerUtil;
import com.mybatisplus.demo.android.viewobject.HeaderFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class HeaderFileServiceimpl implements HeaderFileService {

    @Autowired
    private HeaderFileMapper headerFileMapper;//与dao层进行交互

    /**
     * 头像唯一 当userId不为空 并且header存在 则更新头像
     *
     * @return
     */
    @Override
    public ApiResponse setHeader(String fileSize, String userId, String flePath) {
        LoggerUtil.info("---fileSize---" + fileSize, HeaderFileServiceimpl.class);
        LoggerUtil.info("---userId---" + userId, HeaderFileServiceimpl.class);
        LoggerUtil.info("---flePath---" + flePath, HeaderFileServiceimpl.class);
        ApiResponse apiResponse = new ApiResponse();

        HeaderInfo headerInfo1 = new HeaderInfo();
        headerInfo1.setUserId(userId);

        //查询这条地址是否存在
        QueryWrapper<HeaderInfo> queryWrapper = new QueryWrapper<>(headerInfo1);
        HeaderInfo headerInfo = headerFileMapper.selectOne(queryWrapper);
        if (null == headerInfo) {
            headerInfo1.setFile_size(fileSize);
            headerInfo1.setFile_path(flePath);
            headerInfo1.setCreate_date("" + LocalDateTime.now());
            headerInfo1.setUpdate_date("" + LocalDateTime.now());
            //插入用户
            int i = headerFileMapper.insert(headerInfo1);

            if (i > 0) {
                LoggerUtil.info("---头像插入成功---", HeaderFileServiceimpl.class);
                HeaderFileVo headerFileVo = new HeaderFileVo();
                headerFileVo.setUserId(userId);
                headerFileVo.setFilePath(flePath);
                apiResponse.setResultCode(ResultCode.SUCCESS);
                apiResponse.setData(headerFileVo);
            } else {
                LoggerUtil.info("---头像插入失败---", HeaderFileServiceimpl.class);
                apiResponse.setResultCode(ResultCode.ERROR);
            }
        } else {
            //如果存在则更新这条数据信息
            //不写queryWrapper条件 会变更全部
            UpdateWrapper fileUpdateWrapper = new UpdateWrapper();
            fileUpdateWrapper.eq("user_id", userId); //不写queryWrapper条件 会变更全部
            headerInfo.setFile_size(fileSize);
            headerInfo.setFile_path(flePath);
            headerInfo.setUpdate_date("" + LocalDateTime.now());
            int i = headerFileMapper.update(headerInfo, fileUpdateWrapper);
            if (i > 0) {
                LoggerUtil.info("---头像更新成功---", HeaderFileServiceimpl.class);
                HeaderFileVo headerFileVo = new HeaderFileVo();
                headerFileVo.setUserId(userId);
                headerFileVo.setFilePath(flePath);
                apiResponse.setResultCode(ResultCode.SUCCESS);
                apiResponse.setData(headerFileVo);
            } else {
                LoggerUtil.info("---头像更新失败---", HeaderFileServiceimpl.class);
                apiResponse.setResultCode(ResultCode.ERROR);
            }
        }


        return apiResponse;
    }


    @Override
    public ApiResponse queryHeader(String user_id) {


        return null;
    }


}
